<?php
defined('_JEXEC') or die;
if(isset($this->data->groups[5]))://[V]user is teacher
?>
<div class="wrapper" style="min-height:500px">
	<div class="content" id="createAccount-page">
		<div class="col-wrapper clearfix">
			<div class="list-user clearfix">
				<ul class="breadcrumb2 mb10">
					<li><a href="#">Mine Brugere</a></li>
				</ul>
				<div class="search">
				<form name="studentByName" action="index.php" method="post">
					<input name="name" placeholder="Skriv et navn…"<?php if($this->search) echo ' value="',$this->search,'"'?>>
					<a class="iconSearch" onclick="document.studentByName.submit()">Go</a>
					<input type="submit" style="position: absolute; left:-9999px"/>
					<input type="hidden" name="option" value="com_users" />
					<input type="hidden" name="view" value="profile" />
					<?php echo JHtml::_('form.token'); ?>
				</form>
				</div>
				<div class="clear"></div>
				<ul class="user clearfix">
<?php
$i=0;
foreach($this->items as $item):
?>
					<li<?php if($i) echo ' class="mr0"'?>>
						<a href="#">
						   <?php
						      $dbo=JFactory::getDbo();
						      $query="select * from #__student_month WHERE id_user=".$item->id."";
						      $dbo->setQuery($query);
						      $src=$dbo->loadObject()->image;
						   ?>
						   <img src="images/student/<?php echo $src?>" alt="" width="110" height="141">
						</a>
						<p class="mb0">Profil-nr. <span><?php echo str_pad( $item->id,5 , 0, STR_PAD_LEFT)?></span></p>
						<p class="mb0">Navn: <span><?php echo $item->name?></span></p>
						<p class="mb3">E-mail: <span><a href="mailto:<?php echo $item->email?>" <?php echo $item->email?></a></span></p>
						<a class="btnSee-account" href="index.php?option=com_users&view=profile&layout=student&user_id=<?php echo $item->id?>">Se Konto</a>
						<a class="btnSkype" href="skype:<?php echo $item->skype?>?chat">Skype</a><br><br>
						<a class="btnSee-profile" href="index.php?option=com_users&view=profile&layout=student_checkplan&user_id=<?php echo $item->id?>">Se Profil</a>
						<a class="btnNote btn<?php echo $item->id?>" data-reveal-id="f-note" title="<?php echo $item->note?>" onclick="loadnote(<?php echo $item->id?>, '<?php echo $item->name?>',this.title)">Note</a>
					</li>
<?php
$i=1-$i;
endforeach;?>
				</ul>
			</div>
		</div>
	</div>
</div>

<div id="f-note" class="reveal-modal">
	<div class="f-note clearfix">
		<form id="nform">
			<h1 id="ntitle">&nbsp;</h1>
			<div class="login-event clearfix">
				<div class="event-content clearfix">
					<textarea name="note" placeholder="Dette eren"></textarea>
				</div><!--.event-content-->
				<a class="btnSave2" onclick="$('#close-reveal-modal').click()">Gem</a>
			</div>
			<input type="hidden" name="option" value="com_users" />
			<input type="hidden" name="uid" />
			<input type="hidden" name="task" value="user.takenote" />
			<?php echo JHtml::_('form.token'); ?>
		</form>
	</div>
	<a id="close-reveal-modal" class="close-reveal-modal"></a>
</div>
<script type="text/javascript">
	function loadnote(d, n, s){
		$("#ntitle").html(n+": note");
		$("#nform")[0].note.value=s;
		$("#nform")[0].uid.value=d;
		$(".btnSave2").click(function(){savenote(d)});
	}
	function savenote(d){
		dat=$("#nform").serialize();
		$.post('<?php echo JURI::root()?>index.php?', dat+'&no_html=1', function(){
				$(".btn"+d).attr("title",$("#nform")[0].note.value);//update the note
			});
	}
</script>
<?php


else://									Student layout

$m_start=intval(substr($this->data->registerDate,5,2));
$y_start=intval(substr($this->data->registerDate,0,4));
$m_now=intval(date("m"));		$y_now=intval(date("Y"));
$tpath=JURI::base().'templates/'.JFactory::getApplication()->getTemplate();

if($y_start!=$y_now)
	$m_now+=12;

$map=array(0,'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December',
'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December');
$period= 12;

	function mySql2PhpTime($sqlDate){
		$arr = date_parse($sqlDate);
		return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);
	}

$db=JFactory::getDbo();
$db->setQuery("SELECT * FROM #__student_month WHERE id_user=".$this->data->id." ORDER BY id");
$rows=$db->loadObjectList();//monthly data

//count items
$db->setQuery("SELECT registerDate FROM `#__users` WHERE id=".$this->data->id);
$regDate=$db->loadResult();$regDate=mySql2PhpTime($regDate);
$regYear=(int)date("y", $regDate);	$regDate=(int)date("W", $regDate);//regDate become regWeek

$db->setQuery("SELECT id,StartTime,EndTime,Color,RecurringRule FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=0");
$items=$db->loadObjectList();//items for counting
$yes=0;$no=0;

foreach($items as $item){

	if($item->Color==10)
		$yes++;
	else if($item->Color==2)
		$no++;
	//natural count, real items

	if($item->RecurringRule){

		$item->RecurringRule=json_decode($item->RecurringRule);

		$sDate=mySql2PhpTime($item->StartTime);
		$sYear=(int)date("y", $sDate);
		$sWeek=(int)date("W", $sDate);

		$weeks=$item->RecurringRule->weeks;
		$weekdays=$item->RecurringRule->weekdays;

		if($regYear != $sYear)//is the same year?
			$sWeek=52+$sWeek;

		if(isset($item->RecurringRule->data)){

			$tmp=0;$tmp1=0;
			foreach($item->RecurringRule->data as $dat)
				foreach($dat as $dat0)
					if($dat0==10)
						$tmp1++;
					else if($dat0==2)
						$tmp++;

			$yes+=$tmp1;$no+=$tmp;
		}
	}
}///count

$db->setQuery("SELECT StartTime FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=1");
$tmp=$db->loadResultArray();//load meetings
$now=time();

foreach($tmp as $v){
	$v=mySql2PhpTime($v);
	if($v && $v >= $now){
		$d=date("d.m.Y", $v);
		$tmp=$d. " Kl. " .date("H.i", $v);
		break;
	}
}
if(is_array($tmp))//still array? die
	$tmp="";

?>
<h2 class="titlepage">Personliag profil</h2>
<div class="eachBox min-profil-box clearfix">
	<div class="cont-box wrap-profit-top">
		<div class="info-box clearfix">
			<div class="divinfo info-01">
				<p>
					Profil-nr. <?php echo $this->data->id;?>
				</p>
				<p>
					Navn: <?php echo $this->data->name;?>
				</p>
				<p>
					Startvægt: <?php echo $this->data->Vægt;?> kg.
				</p>
			</div>
			<div class="divinfo info-02">
				<p>
					Personlig vejleder: <?php echo $this->data->teacher_name?>
				</p>
				<p>
					Næste webmøde: <?php echo $tmp?>
				</p>
				<!--<p>
					<a class="btnYellow btnRediger-brug" href="<?php echo JRoute::_("index.php?option=com_users&view=profile&layout=edit&Itemid=117")?>">
						Rediger brugeroplysninger
					</a>
				</p>-->
			</div>
		</div>
<!--start monthly view-->
		<div class="accodion-info-box clearfix">
<?php //Month tabs
	for($i=$m_start,$j=0;$j<$period;$j++,$i++){
		$tmp=explode(";", $rows[$j]->event);
		if(!empty($w[0])) $lastw= $w[0];
?>
			<div class="box-month clearfix">
			<form method="post" action="" id="save_month<?php echo $i;?>" enctype="multipart/form-data">
				<p class="p-month">
					<span class="text">
						<?php echo $map[$i]?>
					</span>
					<span class="iconDown">
						<img src="templates/fitplan/src_mobile/images/iconDown.png">
					</span>
				</p>
				<div class="box-infomonth clearfix" style="display: <?php echo ($i==$m_now) ? 'block' : 'none'?>;">
<?php if($m_now==$i){
	//Upload image available on current month
	?>
							<h4 class="result"><a>Opload billede</a></h4>
							<input type="file" id="fileimg" style="position: absolute;margin-top: -30px;width: 102px;opacity:0" name="image_student" />
<?php }?>
					<div class="question-mnt">
<?php
//statistic data
$w=array_map(function($item){$item=explode(":",$item);return $item[1];}, $tmp);
?>
						<p class="clearfix">
							<label class="lb-mnt">
								Indtast denne måned vægt:
							</label><input class="input_vu" value=" kg" disabled=""><input class="txtip input_nam" value="<?php echo $w[0] ? : ($i==$m_start ? $this->data->Vægt : "")?>" name="vagt">
						</p>
						<p class="clearfix">
							<label class="lb-mnt">
								Indtast mål på talje:
							</label><input class="input_vu" value=" cm" disabled=""><input class="txtip input_nam" value="<?php echo $w[1]?>" name="talje">
						</p>
						<p class="clearfix">
							<label class="lb-mnt">
								Indtast mål på hofte:
							</label><input class="input_vu" value=" cm" disabled=""><input class="txtip input_nam" value="<?php echo $w[2]?>" name="hofte">
						</p>
						<p class="clearfix">
							<label class="lb-mnt">
								Indtast mål på bryst:
							</label><input class="input_vu" value=" cm" disabled=""><input class="txtip input_nam" value="<?php echo $w[3]?>" name="bryst">
						</p>
						<p class="clearfix">
							<label class="lb-mnt">
								Indtast mål på overarm:
							</label><input class="input_vu" value=" cm" disabled=""><input class="txtip input_nam" value="<?php echo $w[4]?>" name="bryst1">
						</p>
					</div>
					<div class="wrap-kg clearfix">
						<div class="row-kg">
							<p>
								Kg. tilbage til slutmålet:
								<span>
									<?php echo $w[0] ? $this->data->information['weightdown'] - $this->data->Vægt + $w[0] : ($i==$m_start ? $this->data->information['weightdown'] : '-')//how much weight remain?> kg.
								</span>
							</p>
							<p>
								Tabt siden start:
								<span>
									<?php echo !empty($w[0]) ? $this->data->Vægt-$w[0] : '-'//offset compare to goal?> kg.
								</span>
							</p>
							<p>
								Tabt siden sidste måned:
								<span>
									<?php echo $lastw && !empty($w[0]) ? $lastw-$w[0] : '-'//offset compare to last month?> kg.
								</span>
							</p>
						</div>
					</div>

					<div class="div-bottom clearfix">
						<div class="row-note clearfix">
							<p>
								Noter
							</p>
							<textarea class="txtta" name="note"><?php
								echo !empty($rows[$j]->note) ? $rows[$j]->note : "Uge 1:

Uge 2:

Uge 3:

Uge 4:";
								?></textarea>
						</div>
						<div class="avata-sub ">
<?php //auto fit box
if($rows[$j]->image)
	$tmp1='images/student/'.$rows[$j]->image;
$h='';
if(file_exists($tmp1))
{
	list( $ww, $h)=getimagesize($tmp1);
	if($h>$ww){
		//if($h>240)
			$h='height="240"';
	}else{
		//if($ww > 185)
			$h='width="185"';
	}
}else{
	$h='height="240" width="185"';
}
?>
								<img <?php if($m_now==$i){?>id="preview"<?php }?> <?php echo $h?> <?php echo 'src="'.JURI::root(),$tmp1,'"'?> alt="portrait (size 185x240)">
						</div>
						<div class="divResult">
							<p>
								Resultat af tjekplan:
							</p>
							<p class="green">
								JA (<?php echo $yes?>)
							</p>
							<p class="red">
								NEJ (<?php echo $no?>)
							</p>
						</div>
					</div>

					<p class="wrapbtnGem a-center">
						<a class="btnYellow btnGem" onclick="jQuery('#save_month<?php echo $i?>').submit();">
							Gem
						</a>
					</p>
				</div>
	<input type="hidden" value="<?php echo $i?>" name="month"/>
	<input type="hidden" value="com_users" name="option"/>
	<input type="hidden" value="user.save_month_student" name="task"/>
	<input type="hidden" value="<?php echo $this->data->id?>" name="id_user"/>
				</form>
			</div><!--box-month-->
<?php
	}?>
		</div>
		
		
		
		
		
		
	</div>
</div>
<script type="text/javascript" src="templates/fitplan/src_mobile/js/js.footer.tho.js"></script>
<?php endif;//[V] end student layout?>